package leetcode;

public class MyLink {

    //https://blog.csdn.net/superxiaolong123/article/details/86687733
    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        ListNode d = new ListNode(4);
        ListNode e = new ListNode(5);

        a.next = b;
        b.next = c;
        c.next = d;
        d.next = e;

        System.out.println(a.toString());
        System.out.println(reverse(a).toString());
    }

    //翻转链表
    public static ListNode reverse(ListNode root) {
        ListNode result = new ListNode(0);
        result.next = root;
        //root是最后的尾节点
        while (root.next != null) {
            ListNode tmp = root.next;
            if (root.next.next != null)
                root.next = root.next.next;
            else
                root.next = null;
            tmp.next = result.next;
            result.next = tmp;
        }
        return result.next;
    }

    //别人的 窝日明天研究吧
    public static ListNode reverseListByInsert(ListNode listNode){
        //定义一个带头节点的
        ListNode resultList = new ListNode(-1);
        //循环节点
        ListNode p = listNode;
        while(p!= null){
            //保存插入点之后的数据
            ListNode tempList = p.next;
            p.next = resultList.next;
            resultList.next = p;
            p = tempList;
        }
        return resultList.next;
    }
}
